
package com.probiz.estore.imports.web.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.Constants;
import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.core.controller.BaseController;
import com.probiz.estore.core.util.ContextUtil;
import com.probiz.estore.core.view.DownloadView;
import com.probiz.estore.imports.model.ImportInfo;
import com.probiz.estore.imports.service.BaseImportManager;
import com.probiz.estore.webapp.util.RequestContext;

public abstract class BaseImportController extends BaseController {
	
protected BaseImportManager	importManager;
	
	/**
	 * 子类可覆盖该方法定制特殊任务界面
	 * @return
	 */
	protected ModelAndView getTaskView(){
		ModelAndView mv=new ModelAndView("imports/task");
		return mv;
	}
	/**
	 * 子类可覆盖该方法定制任务情况界面
	 * @return
	 */
	protected ModelAndView getTaskStatusView(){
		ModelAndView mv=new ModelAndView("imports/importStatus");
		return mv;
	}
	/**
	 * 子类需覆盖该方法获得文件模板url
	 * @return
	 */
	abstract protected String getFileTemplateUrl();
	/**
	 * 子类需覆盖该方法获得文件例子url
	 * @return
	 */
	abstract protected String getFileExampleUrl();
	
	/**
	 * 子类需覆盖该方法定制html的title
	 * @return
	 */
	abstract protected String getTitle();

	/** 
	 * 输入导入数据的相关信息
	 **/
	public ModelAndView defaultAction(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		ImportInfo importInfo = importManager.getImportInfo();
		ModelAndView mv = null;
		if(null==importInfo){
			//当前没有任务
			mv=getTaskView();
			mv.addObject("encodings", importManager.getImportConfig().getEncodings());
			mv.addObject("fileTemplateUrl", getFileTemplateUrl());
			mv.addObject("fileExampleUrl", getFileExampleUrl());
		}else{
			mv = getTaskStatusView();
		}
		mv.addObject("title", getTitle());
		return mv;
	}

	/**
	 * 下载范文和例子
	 **/
	public ModelAndView downloadFile(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		String path = ContextUtil.getServletContext().getRealPath(request.getParameter("fileUrl"));
		ModelAndView mv = new ModelAndView(new DownloadView(path));
		return mv;
	}
	
	/**
	 * 下载日志、匹配文件等
	 **/
	public ModelAndView downloadMedia(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		String path = ConfigUtil.getInstance().getMediaStorePath(request.getParameter("fileUrl"));
		ModelAndView mv = new ModelAndView(new DownloadView(path));
		return mv;
	}
	
	
	/**
	 * 进入导入文件预览页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView preview(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		String importFile = request.getParameter("importFile");
		String encoding = request.getParameter("encoding");
		try {
			importFile=URLDecoder.decode(importFile,Constants.DEFAULT_ENCODING);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		ModelAndView mv = new ModelAndView("imports/preview");
		List<List<String>> result=importManager.preview(importFile,encoding);
		Integer rowCount = importManager.countFileRowSize(importFile,encoding);
		mv.addObject("rowCount", rowCount);
		mv.addObject("previewFileData", result);
		return mv;
	}
	/**
	 * 获得导入文件列数
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView countFileRowSize(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		Map<String,Object> data = new HashMap<String,Object>();
		String importFile = request.getParameter("importFile");
		String encoding = request.getParameter("encoding");
		try {
			importFile=URLDecoder.decode(importFile,Constants.DEFAULT_ENCODING);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Integer rowCount = importManager.countFileRowSize(importFile,encoding);
		data.put("rowCount", rowCount);
		return this.getModelAndAjaxJsonView(data);
	}
	
	/**
	 * 开始导入
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView startImport(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		String importFile=request.getParameter("importFile");
		String encoding=request.getParameter("encoding");
		try {
			importFile=URLDecoder.decode(importFile,Constants.DEFAULT_ENCODING);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		importManager.startImport(importFile, encoding, RequestContext.getCurrentUserId());
		/*ModelAndView mv = getTaskStatusView();
		mv.addObject("title", getTitle());
		return mv;*/
		return new ModelAndView("redirect:"+request.getServletPath()+"?doAction=showView");

	} 
	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView showView(HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView mv = getTaskStatusView();
		mv.addObject("title", getTitle());
		return mv;
		
	}

	/**
	 * 获取导入信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView getImportInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		return this.getModelAndAjaxJsonView(importManager.getImportInfo().getJsonModel());
	}
	/**
	 * 停止
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView stopImport(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		importManager.stopImport();
		return this.getModelAndAjaxJsonView(importManager.getImportInfo().getJsonModel());
	}
	/**
	 * 暂停
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView suspendImport(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		importManager.suspendImport();
		return this.getModelAndAjaxJsonView(importManager.getImportInfo().getJsonModel());
	}
	/**
	 * 继续
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView resumeImport(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		importManager.resumeImport();
		return this.getModelAndAjaxJsonView(importManager.getImportInfo().getJsonModel());
	}
	/**
	 * 重新开始
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView restartImport(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		if(null!=importManager.getImportInfo()){
			importManager.startImport(importManager.getImportInfo().getImportFileUrl(), importManager.getImportInfo().getEncoding(), RequestContext.getCurrentUserId());
		}
		ModelAndView mv = getTaskStatusView();
		mv.addObject("title", getTitle());
		return mv;
	}
	/**
	 * 新任务
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView newTask(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		importManager.newTask();
		ModelAndView mv=getTaskView();
		mv.addObject("encodings", importManager.getImportConfig().getEncodings());
		mv.addObject("fileTemplateUrl", getFileTemplateUrl());
		mv.addObject("fileExampleUrl", getFileExampleUrl());
		mv.addObject("title", getTitle());
		return mv;
	}
	
	
	
}
